package com.github.iot.activity;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.SystemSetting;
import com.github.iot.entity.User;
import com.github.iot.utils.ActivityUtils;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.FastJsonUtils;
import com.github.iot.utils.SharedPreferencesUtils;
import com.github.iot.utils.ToastUtils;
import com.github.iot.view.RxTitle;
import com.mylhyl.circledialog.CircleDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 个人信息Activity
 *
 * @Author: 梁声洪
 * @Date: 2019/12/09 9:14
 * @Version: v1.0.0
 */
public class UserInfoActivity extends AppCompatActivity {

    @BindView(R.id.rx_title)
    RxTitle mTitle;

    @BindView(R.id.tv_nickname)
    TextView mNickname;

    @BindView(R.id.tv_username)
    TextView mUsername;

    @BindView(R.id.tv_type)
    TextView mType;

    @BindView(R.id.tv_system_name)
    TextView tvSystemName;

    private SystemSetting setting;
    private CommonDaoUtils<SystemSetting> settingDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_user_info);

        ButterKnife.bind(this);

        initView();
        initData();
    }

    private void initView() {
        mTitle.setLeftFinish(this);

        String str = SharedPreferencesUtils.getString(this, "iot_login_user", null);
        if (null != str) {
            User user = FastJsonUtils.strToBean(str, User.class);
            mNickname.setText(user.getNickname());
            mUsername.setText(user.getUsername());
            if (1 == user.getType()) {
                mType.setText("超级管理员");
            } else if (2 == user.getType()) {
                mType.setText("管理员");
            } else if (3 == user.getType()) {
                mType.setText("普通用户");
            }
        }
    }

    private void initData() {
        settingDao = DaoUtilsStore.getInstance().getSystemSettingDaoUtils();
        List<SystemSetting> list = settingDao.queryAll();
        if (list.size() > 0) {
            setting = list.get(0);
            tvSystemName.setText(setting.getName());
        }
    }

    @OnClick({R.id.btn_password, R.id.ll_system_name})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_password:
                ActivityUtils.skipActivity(UserInfoActivity.this, UpdateUserInfoActivity.class);
                break;
            case R.id.ll_system_name:
                updateSystemName();
                break;
            default:
                break;
        }
    }

    private void updateSystemName() {
        new CircleDialog.Builder()
                .setCanceledOnTouchOutside(false)
                .setCancelable(true)
                .setTitle("修改系统名称")
                .setInputHint("请输入系统名称")
                .setInputText(setting.getName())
                .setInputHeight(95)
                .setInputShowKeyboard(true)
                .setCancelable(false)
                .configInput(params -> params.styleText = Typeface.BOLD)
                .setNegative("取消", null)
                .setPositiveInput("确定", (text, v) -> {
                    if (TextUtils.isEmpty(text)) {
                        ToastUtils.warning("请输入系统名称");
                        return false;
                    } else {
                        setting.setName(text);
                        boolean flag = settingDao.update(setting);
                        if (flag) {
                            tvSystemName.setText(setting.getName());
                            ToastUtils.success("操作成功");
                        } else {
                            ToastUtils.error("操作失败");
                        }
                        return true;
                    }
                })
                .show(getSupportFragmentManager());
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }

}